#include "xmlusers.h"
/*
UsersHandler::UsersHandler(Users &usrs) : users(usrs) {
}

UsersHandler::~UsersHandler() {
    saveToFile();
}

bool UsersHandler::startDocument() {
    return true;
}

bool UsersHandler::endDocument() {
    return true;
}

bool UsersHandler::startElement(const QString&, const QString&, const QString &qName,
const QXmlAttributes&)
{
    tags.push(qName);
    return true;
}

bool UsersHandler::endElement(const QString&, const QString&, const QString& qName)
{
    if(qName == tags.top()) {
        tags.pop();
    }
    return true;
}

bool UsersHandler::characters(const QString &ch) {
    //bool ok = false;
    QString val = ch.trimmed();

    if(val.isEmpty()) {
        return true;
    }

    QString currentTag = tags.top();

    if(currentTag == UsersTags::USER) {
        users.addUser(val);
    }

    return true;
}

bool UsersHandler::fatalError(const QXmlParseException& exc) {
    qDebug() << exc.message();
    return true;
}

void UsersHandler::loadFromFile(const QString &path) {
    QFile xmlFile(path);
    if(!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        //TODO throw exception
    } else {
        filePath = path;

        QXmlInputSource src(&xmlFile);

        QXmlSimpleReader reader;
        reader.setContentHandler(this);
        reader.setErrorHandler(this);

        bool ok = reader.parse(src);
        if(!ok) {
            //TODO error
        }

        users.sort();

        xmlFile.close();
    }
}

void UsersHandler::saveToFile() {
    QFile xmlFile(filePath);
    if(!xmlFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
        //TODO throw exception
    } else {
        QXmlStreamWriter writer(&xmlFile);
        writer.setAutoFormatting(true);
        writer.writeStartDocument("1.0");

        writer.writeStartElement(UsersTags::USERS);

        foreach(QString user, users.getUsers()) {
            writer.writeTextElement(UsersTags::USER, user);
        }

        writer.writeEndElement();

        writer.writeEndDocument();
        xmlFile.close();
    }
}
*/
/* ************************************************************************************ */
/*
const QString UsersTags::USERS = "wl-users";
const QString UsersTags::USER = "wl-user";
*/
